<?php

namespace app\common\model;

use think\Model;
use Db;

class Role extends Model
{

    protected $table = 'qi_role';

    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    public function getInfoById($id){
        $info = $this->where('id', $id)->find();
        return $info;
    }

    public function getPageList($pagesize=10){
        $list = $this->order('id', 'desc')->paginate($pagesize);

        return $list;
    }

    /*
     * 查询指定角色下的菜单
     */
    public function getRoleMenuById($role_id){
        $roleMenu= Db::table('qi_role_menu')->where('role_id', $role_id)->select();
        $menuIds = [];
        foreach ($roleMenu as $item){
            $menuIds[] = $item['menu_id'];
        }
        if(empty($menuIds)){
            return [];
        }

        $menuDatas = Db::table('qi_menu')->where('id','IN', $menuIds)->select();
        $menuNames = [];
        foreach($menuDatas as $item){
            $menuNames[] = $item['name'];
        }
        return $menuNames;
    }

    /*
     * 查询指定角色下的用户
     */
    public function getRoleUserById($role_id){
        $roleUser =  Db::table('qi_role_user')->where('role_id', $role_id)->select();
        $userIds = [];
        foreach ($roleUser as $item){
            $userIds[] = $item['user_id'];
        }
        if(empty($userIds)){
            return [];
        }

        $userDatas = Db::table('qi_user')->where('id','IN', $userIds)->select();
        $userNames = [];
        foreach($userDatas as $item){
            $userNames[] = $item['username'];
        }
        return $userNames;
    }



    /*
    * 添加角色
    */
    public function addrole($info){

        $this->data($info);

        $result = $this->save();
        return $result;
    }

    /*
     * 删除角色
     * 将关联的数据表中相关数据都删除
     */
    public function delrole($id){
        $this->where('id', $id)->delete();
        Db::table('qi_role_menu')->where('role_id', $id)->delete();
        Db::table('qi_role_user')->where('role_id', $id)->delete();

        return true;
    }



    /*
     * 获取角色关联菜单
     */
    public function getRoleMenu($role_id){
        $menuDatas = Db::table('qi_role_menu')
            ->where('role_id', $role_id)
            ->select();

        $menuIds = [];
        foreach($menuDatas as $item){
            $menuIds[] = $item['menu_id'];
        }

        return $menuIds;
    }

    /*
     * 更新角色关联菜单
     */
    public function updateRoleMenu($role_id, $menu_ids){
        Db::table('qi_role_menu')
            ->where('role_id', $role_id)
            ->delete();

        $insertDatas = [];
        foreach($menu_ids as $item){
            $insertDatas[] = [
                'role_id'=>$role_id,
                'menu_id'=>$item
            ];
        }
        if($insertDatas){
            Db::table('qi_role_menu')->insertAll($insertDatas);
        }
    }

    /*
     * 获取角色关联用户
     */
    public function getRoleUser($role_id){
        $userDatas = Db::table('qi_role_user')
            ->where('role_id', $role_id)
            ->select();

        $userIds = [];
        foreach($userDatas as $item){
            $userIds[] = $item['user_id'];
        }

        return $userIds;
    }

    /*
     * 更新角色用户关联
     */
    public function updateRoleUser($role_id, $user_ids){
        Db::table('qi_role_user')
            ->where('role_id', $role_id)
            ->delete();
        $insertDatas = [];
        foreach($user_ids as $item){
            $insertDatas[] = [
                'role_id'=>$role_id,
                'user_id'=>$item
            ];
        }
        if($insertDatas){
            Db::table('qi_role_user')->insertAll($insertDatas);
        }
    }

}